package com.sky.dish.controller;

import com.sky.dish.service.DishService;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 菜品管理 controller
 */
@Slf4j
@Validated
@Api(tags = {"菜品管理"}, description = "后台菜品操作")
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Resource
    private DishService dishService;



    /**
     * 菜品管理
     *        新增菜品 及 口味
     * @param dishDTO
     */
    @ApiOperation(tags = {"菜品管理"}, value = "新增菜品", notes = "数据库新增菜品信息")
    @PostMapping
    public Result addDish(@RequestBody @Validated DishDTO dishDTO){
        log.info("菜品管理 - 新增菜品: {}", dishDTO);
        dishService.saveDishAndFlavor(dishDTO);
        return Result.success();
    }



    /**
     * 菜品管理
     *        菜品分页条件查询
     * @param dishPageQueryDTO
     */
    @ApiOperation(tags = {"菜品管理"}, value = "菜品分页条件查询", notes = "菜品分页-条件查询列表")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品管理 - 分页条件查询: {}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }



    /**
     * 菜品管理
     *        删除菜品 及 口味
     */
    @ApiOperation(tags = {"菜品管理"}, value = "根据id删除菜品", notes = "根据id删除禁用、未关联套餐的菜品以及口味")
    @DeleteMapping
    public Result deleteDishAndFlavor(@NotEmpty(message = "id为空") @RequestParam(name="ids")List<Long> ids){
        log.info("菜品管理 - 根据id删除菜品: {}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }



    /**
     * 菜品管理
     *        根据id查询菜品 及 关联口味
     */
    @ApiOperation(tags = {"菜品管理"}, value = "根据id查询菜品及口味", notes = "根据id查询菜品以及口味数据")
    @GetMapping("/{id}")
    public Result selectDishAndFlavorByDishId(@PathVariable(name="id")Long id){
        log.info("菜品管理 - 根据id: {},查询菜品以及关联口味", id);
        DishVO dishVO = dishService.selectDishAndFlavorByDishId(id);
        return Result.success(dishVO);
    }



    /**
     * 菜品管理
     *        修改菜品 及 口味
     */
    @ApiOperation(tags = {"菜品管理"}, value = "修改菜品信息以及口味", notes = "修改菜品信息以及口味信息")
    @PutMapping
    public Result updateDishAndFlavor(@RequestBody @Validated DishDTO dishDTO){
        log.info("菜品管理 - 修改菜品及口味: {}", dishDTO);
        dishService.updateDishAndFlavor(dishDTO);
        return Result.success();
    }



    /**
     * 菜品管理
     *       菜品起售、停售
     */
    @ApiOperation(tags = {"菜品管理"}, value = "菜品起售、停售", notes = "菜品起售、停售修改")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable(name="status")Integer status, @NotNull(message = "id为空") Long id){
        log.info("菜品管理 - 菜品id: {},当前操作(0停售/1起售): ==> {}", id, status);
        dishService.updateStatus(id, status);
        return Result.success();
    }



    /**
     * 菜品管理
     *       根据分类id查询菜品列表
     */
    @ApiOperation(tags = {"菜品管理"}, value = "根据菜品分类查询", notes = "根据菜品分类查询，返回某类数据集合")
    @GetMapping("/list")
    public Result selectDishListByCategoryId(@NotNull(message = "分类id为空")Long categoryId){
        log.info("菜品管理 - 根据分类id: {}查询菜品列表", categoryId);
        List<Dish> dishList = dishService.selectDishListByCategoryId(categoryId);
        return Result.success(dishList);
    }





}
